/* 
    response.end 返回table页面进行渲染
*/

const fs = require("fs");
const path = require("path");
const http = require("http");
const logger = require("../_unit/logs");

let filePath = path.resolve(__dirname, "./05/index.html");
const server = http.createServer((request, response) => {
  //   let {pathname} = new URL(request.url, "http://localhost:3000");
  //   logger.info(pathname, "headers");

  // 读取 【05_http相应练习.html】 文件内容
  let file = mathFile(request.url);
  let fileBuff = fs.readFileSync(file);

  response.end(fileBuff);
});

server.listen(3000, () => {
  console.log("开启3000服务!");
});

function mathFile(url) {
  let filePath = null;
  if (url == "/") {
    filePath = path.resolve(__dirname, "./05/index.html");
    return filePath;
  }
  if (url.includes("css")) {
    filePath = path.resolve(__dirname, "./05/index.css");
    return filePath;
  }
  if (url.includes("js")) {
    filePath = path.resolve(__dirname, "./05/index.js");
    return filePath;
  }
  if (url.includes("png")) {
    filePath = path.resolve(__dirname, "./05/a.png");
    return filePath;
  }
}
